import { defineStore } from "pinia";
import defaultSettings from "@/settings";

import i18n from "@/lang/index";

import { store } from "@/stores";

// setup
export const useAppStore: any = defineStore("app", () => {
  // state
  const size = useStorage("size", defaultSettings.size);
  const language = useStorage("language", defaultSettings.language);
  const activeTopMenuPath = useStorage("activeTopMenuPath", "");
  /**
   * 根据语言标识读取对应的语言包
   */
  const locale = computed(() => {
    if (language?.value == "en") {
      return i18n.global.getLocaleMessage("en");
    } else {
      return i18n.global.getLocaleMessage("zh-cn");
    }
  });

  function changeSize(val: string) {
    size.value = val;
  }
  /**
   * 切换语言
   *
   * @param val
   */
  function changeLanguage(val: string) {
    language.value = val;
  }
  /**
   * 混合模式顶部切换
   */
  function activeTopMenu(val: string) {
    activeTopMenuPath.value = val;
  }
  return {
    language,
    locale,
    size,
    activeTopMenu,
    changeSize,
    changeLanguage,
    activeTopMenuPath
  };
});

// 手动提供给 useStore() 函数 pinia 实例
// https://pinia.vuejs.org/zh/core-concepts/outside-component-usage.html#using-a-store-outside-of-a-component
export function useAppStoreHook() {
  return useAppStore(store);
}
